﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Topology;

using DotSpatial.Topology.Operation.Polygonize;

using System.Collections;

using FXnaInfrastructure;

namespace GWApplication
{

    /// <summary>
    /// 设计用于保存拓扑类型图层
    /// </summary>
    [Serializable]
    public class TLayer:FEntity
    {


        public TLayer(string id):base(id)
        {

            this._nodeIds = new IDGenerator(0,"Node");

            this._lineIds = new IDGenerator(0,"Line");


 
        }



        IDGenerator _nodeIds;

        public IDGenerator NodeIds
        {
            get { return _nodeIds; }
            
        }


        IDGenerator _lineIds;

        public IDGenerator LineIds
        {
            get { return _lineIds; }
           
        }




        ConceptualModel _parentCM;

        public ConceptualModel ParentCM
        {
            get { return _parentCM; }
            set { _parentCM = value; }
        }




        List<TNode> _nodes = new List<TNode>();

        List<TLine> _lines = new List<TLine>();


        #region 事件


        //这个事件，既要对 不同的添加类型负责 ，又要对数量，操作类型负责

        [field:NonSerialized]

        public event EventHandler<GeometryEventArgs> GeometryAdded;

          [field: NonSerialized]
        public event EventHandler<GeometryEventArgs> GeometryModified;

          [field: NonSerialized]
          public event EventHandler<GeometryEventArgs> GeometryRemoved;
             



        #endregion



        #region 函数

        public TNode GetNode(Coordinate coord)
        {
            Point p = new Point(coord);

            foreach (TNode item in this._nodes)
            {
                if (item.Point.Equals(p))
                {

                    return item;
                    
                } 


                
            }


            return null;
        }





        public void AddNode(TNode node)
        {

 

        }




        public void AddLine(TLine line)
        {


 
        }



        /// <summary>
        /// 尝试建立更通用一些的数据接口
        /// 将复杂性封装在内部
        /// </summary>
        /// <param name="c"></param>
        public void AddNode(Coordinate c)
        {

            if (this.GetNode(c)==null)
            {
                TNode node = new TNode(this._nodeIds.GetNextId(),(Coordinate)c.Clone());

                this._nodes.Add(node);
                
            }

 
        }


        public void AddLine(List<Coordinate> coords)
        {

            //需要做比较多的判断

            //这里传入的坐标，是要变化一下的，所以需要克隆

            


            LineString ls = new LineString(coords);

            //根据不同情况，需要切割线条等操作

            //在这里我们简化一下先


            TLine line = new TLine(this.LineIds.GetNextId(),coords.CloneList());

            if (this.GeometryAdded!=null)
            {
                GeometryAdded(this, new GeometryEventArgs(GeomoetryType.LINE, new List<TLine>() { line}));
                
            }


 
        }




        public void BuildPolygon()
        {

            //不得不承认, dotspatial 太给力了



            List<LineString> lines = new List<LineString>();

            Polygonizer polynizer = new Polygonizer();

            polynizer.Add(lines);

            //内部实现即是如此

            ArrayList polys = (ArrayList)polynizer.Polygons;

           // List<IGeometry> lstResult=  polynizer.Polygons;
 
        }



        #endregion


    }
}
